#!/sbin/openrc-run
# Copyright 1999-2016 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

depend() {
	need net
}

checkresolvconf() {
	#make /etc/bfilter/etc/resolv.conf if chroot is set
	if [ "${BFILTER_OPTS#* -r *}" != "${BFILTER_OPTS}" ] ; then
		local CHROOTDIR="${BFILTER_OPTS#* -r }"
		CHROOTDIR="${CHROOTDIR%% *}"
		mkdir -p "${CHROOTDIR}/etc" || return 1
		if ! cmp -s /etc/resolv.conf "${CHROOTDIR}/etc/resolv.conf" ; then
			cp -p /etc/resolv.conf "${CHROOTDIR}/etc/resolv.conf" || return 1
		fi 
	fi

	return 0
}

start() {
	checkresolvconf || return 1

	ebegin "Starting bfilter"
	start-stop-daemon --start --quiet --exec /usr/bin/bfilter -- ${BFILTER_OPTS}
	eend $?
}

stop() {
	ebegin "Stopping bfilter"
	start-stop-daemon --stop --quiet --exec /usr/bin/bfilter
	eend $?
}
